import http from '@/utils/http';
import { languages } from 'monaco-editor';
import json = languages.json;

export interface IGroupCreator {
  id: number;
  username: string;
  avatar: string;
}

export interface IGroup {
  id: number;
  name: string;
  creator: IGroupCreator;
  teamDetail: number;
  // eslint-disable-next-line camelcase
  team_type: number;
  introduction: string;
  memberCount: number;
  createTime: Date;
}

export interface IGroupList {
  list: {
    count: number;
    next: string;
    previous: string;
    results: IGroup[];
  };
}

export interface GroupRoute {
  path: string;
  breadcrumbName: string;
  children?: Array<{
    path: string;
    breadcrumbName: string;
  }>;
}


export const getGroupList = async (other ?: object) => {
  return (await http.get('/group/list/', { ...other })) as IGroupList;
};


export interface IGroupBaseInfo {
  id: number;
  name: string;
  creator: IGroupCreator;
  memberCount: number;
  createTime: Date;
  teamType: number;
}

export interface IGroupDetail {
  id: number;
  background: string;
  baseInfo: IGroupBaseInfo;
  announcement: string;
}

export interface IGroupHeaderInfo {
  id: number;
  background: string;
  name: string;
  memberCount: number;
  createTime: Date;
}

export const getGroupDetail = async (id: number) => {
  return (await http.get(`/group/${id}/`)) as IGroupDetail;
};

export const getGroupHeaderInfo = async (id: number) => {
  return (await http.get(`/group/${id}/header-info/`)) as IGroupHeaderInfo;
};


export interface UpdateGroupAnnouncement {
  announcement: string;
}

export const updateGroupAnnouncement = async (id: number, data: object) => {
  return (await http.put(`/group/${id}/announcement/`, data)) as UpdateGroupAnnouncement;
};

export interface IMemberIndexInfo {
  id: number;
  name: string;
  rating: number;
  rank: number;
  isAdmin: boolean;
  isCreator: boolean;
}

export const getMemberIndexInfo = async (id: number) => {
  return (await http.get(`/group/${id}/member-info/`)) as IMemberIndexInfo;
};


export interface IGroupTrainingIndexItem {
  id: number;
  name: string;
  member: {
    id: number;
    name: string;
    avatar: string;
  };
  status: number;
}


export const getGroupTrainingIndex = async (id: number, data?: object) => {
  return await http.get(`/group/${id}/train/list/`, { params: data }) as { list: IGroupTrainingIndexItem[] };
};


export interface ICurrentTrain {
  id: number;
  name: string;
  // eslint-disable-next-line camelcase
  start_time: Date;
  // eslint-disable-next-line camelcase
  end_time: Date;
  status: number;
}

export const getCurrentTrain = async (id: number, data?: object) => {
  return (await http.get(`/group/${id}/train/todo/`, { params: data })) as { list: ICurrentTrain[] };
};

export interface IMemberSignIn {
  // eslint-disable-next-line camelcase
  record_type: number;
  training: number;
}

export interface IMemberSignInResult {
  // eslint-disable-next-line camelcase
  record_type: number;
  training: number;
  status: number;
}

export const groupMemberSignIn = async (id: number, data: IMemberSignIn) => {
  return (await http.post(`/group/${id}/train/todo/`, data)) as IMemberSignInResult;
};

export interface IMemberTrainingTodo {
  id: number;
  name: string;
  training_type: number;
  start_time: Date;
  end_time: Date;
  recurrence_days: number[];
  status: number;
}

export const getMemberSelfTrainingTodo = async (id: number, data?: object) => {
  return (await http.get(`/group/${id}/train/`, { params: data })) as { list: IMemberTrainingTodo[], count: number };
};


export interface IMemberRemark {
  id: number;
  name: string;
}

export const modifyMemberRemark = async (id: number, data: object) => {
  return (await http.put(`/group/${id}/member-remark/`, data)) as IMemberRemark;
};
